package task3.pimpmypipe;

import java.io.StreamCorruptedException;
import java.security.InvalidParameterException;

public abstract class DataTransformationFilter<T>
  extends AbstractFilter<T, T>
{
  public DataTransformationFilter() {}
  
  public DataTransformationFilter(Readable<T> input, Writeable<T> output)
    throws InvalidParameterException
  {
    super(input, output);
  }
  
  public DataTransformationFilter(Readable<T> input)
    throws InvalidParameterException
  {
    super(input);
  }
  
  public DataTransformationFilter(Writeable<T> output)
    throws InvalidParameterException
  {
    super(output);
  }
  
  public T read()
    throws StreamCorruptedException
  {
    T entity = readInput();
    if (entity != null) {
      process(entity);
    }
    return entity;
  }
  
  public void write(T value)
    throws StreamCorruptedException
  {
    if (value != null) {
      process(value);
    }
    writeOutput(value);
  }
  
  protected abstract void process(T paramT);
  
  public void run()
  {
    try
    {
      writeOutput(read());
    }
    catch (StreamCorruptedException e)
    {
      e.printStackTrace();
    }
  }
}
